package com.example.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.entity.EduSubject;
import com.example.eduservice.entity.excel.SubjectData;
import com.example.eduservice.entity.subject.OneSubject;
import com.example.eduservice.entity.subject.TwoSubject;
import com.example.eduservice.listener.SubjectExcelListener;
import com.example.eduservice.mapper.EduSubjectMapper;
import com.example.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-25
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void savaSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            //文件输入流
            InputStream in = file.getInputStream();

            //调用方法读取                                       这里很重要
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){

            e.printStackTrace();
        }


    }

    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSbuject() {
        // 1查询所有一级分类  parentid=0
        //构造条件
        QueryWrapper<EduSubject> wrapperOne=new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");

        List<EduSubject> onesubjectList = baseMapper.selectList(wrapperOne);

        //这个方法也可以查询this.list();


        //2 查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo=new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");

        List<EduSubject> twosubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储最终封装数据
        List<OneSubject>  finalSubjectList=new ArrayList<>();
        //3 封装一级分类
        /**
            查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
            封装到要求的list集合里面 List《Onesubject》 finalSubjectList

         * */
        //遍历所有一级分类的对象
        for (int i = 0; i < onesubjectList.size(); i++) {

            //new一个新对象用例存取list取到的原始对象
            EduSubject eduSubject=onesubjectList.get(i);

            //new一级分类对象
            OneSubject oneSubject=new OneSubject();

//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //使用spring工具类进行对象复制   将集合中的原始对象数据复制给一级对象
            BeanUtils.copyProperties(eduSubject,oneSubject);

            //新建一个list集合用例存取二级分类
            List<TwoSubject> twofinalSubjectList=new ArrayList<>();

            //遍历二级分类
            //4 封装二级分类
            for (int i1 = 0; i1 < twosubjectList.size(); i1++) {

                //生成一个新对象用例存放二级对象
                EduSubject  tsubject=twosubjectList.get(i1);
                //判断二级对象的父类id和一级的那个相等
                if(tsubject.getParentId().equals(eduSubject.getId())){
                    //新建一个二级分类对象
                    TwoSubject twoSubject=new TwoSubject();
                    //将集合中属于二级分类的原始对象赋值新的二级对象
                    BeanUtils.copyProperties(tsubject,twoSubject);
                    //添加新的二级对象要final集合返回
                    twofinalSubjectList.add(twoSubject);


                }

            }
            //在一级分类里面添加二级分类对象
            oneSubject.setChildren(twofinalSubjectList);
            //将一级分类对象添加到最终返回集合
            finalSubjectList.add(oneSubject);
        }



        return finalSubjectList;
    }
}
